#include "Book.h"
#include "Log.h"
#include "History.h"
#include "GlobalParam.h"

Book::Book(Bpchar FilePath)
{
    Info.BookFilePath = FilePath;
    IsBook = false;

    IsNotesGot = false;
    IsReviewsGot = false;
    IsTagsGot = false;
    IsHistorysGot = false;
}

Book::~Book()
{
    ClearHistorys();
    ClearReviews();
    ClearNotes();
}

Bbool Book::Verify()
{
    size_t pos = 0;
    int cmp1;
    int cmp2;

    pos = Info.BookFilePath.rfind('.');
    cmp1 = Info.BookFilePath.substr(pos+1).compare("pdf");
    cmp2 = Info.BookFilePath.substr(pos+1).compare("txt");

    if(cmp1==0 || cmp2==0)
        IsBook = true;
    else
        IsBook = false;

    bplog(LOG_LEVEL_DEBUG,"Book:verify result:%d",IsBook);

    return IsBook;
}

Bbool Book::GetIsBook()
{
    return IsBook;
}

Bbool Book::GetIsNotesGot()
{
    return IsNotesGot;
}

Bbool Book::GetIsReviewsGot()
{
    return IsReviewsGot;
}

Bbool Book::GetIsTagsGot()
{
    return IsTagsGot;
}

void Book::SetIsNotesGot(Bbool Status)
{
    IsNotesGot = Status;
}

void Book::SetIsReviewsGot(Bbool Status)
{
    IsReviewsGot = Status;
}

void Book::SetIsTagsGot(Bbool Status)
{
    IsTagsGot = Status;
}

void Book::SetIsHistorysGot(Bbool Status)
{
    IsHistorysGot = Status;
}

Bbool Book::GetIsHistorysGot()
{
    return IsHistorysGot;
}

void Book::ClearHistorys()
{
    list<History*>::iterator Itor;

    for(Itor=Historys.begin(); Itor!=Historys.end(); )
    {
        if(*Itor)
            Itor = Historys.erase(Itor);
        else
            Itor++;
    }

    IsHistorysGot = false;
    bplog(LOG_LEVEL_DEBUG,"Book:now historys'length is %d",Historys.size());
}

void Book::ClearReviews()
{
    list<Review*>::iterator Itor;

    for(Itor=Reviews.begin(); Itor!=Reviews.end(); )
    {
        if(*Itor)
            Itor = Reviews.erase(Itor);
        else
            Itor++;
    }

    IsReviewsGot = false;
    bplog(LOG_LEVEL_DEBUG,"Book:now reviews'length is %d",Reviews.size());
}

void Book::ClearNotes()
{
    list<Note*>::iterator Itor;

    for(Itor=Notes.begin(); Itor!=Notes.end(); )
    {
        if(*Itor)
            Itor = Notes.erase(Itor);
        else
            Itor++;
    }

    IsNotesGot = false;
    bplog(LOG_LEVEL_DEBUG,"Book:now notes'length is %d",Notes.size());
}

list<History*>* Book::GetHistorysPtr()
{
    return &Historys;
}

BPcode Book::AddHistory(History* history)
{
    list<History*>::iterator* pItor = NULL;

    pItor = GetHistoryIteratorByID(history->HistoryID);
    if(pItor != NULL)
    {
        bplog(LOG_LEVEL_WARN,"Book:This history already existed in this book by id %d",history->HistoryID);
        return BP_DUPLICATE;
    }

    Historys.push_back(history);
    return BP_OK;
}

list<History*>::iterator* Book::GetHistoryIteratorByID(uint32_t HID)
{
    list<History*>::iterator Itor;

    for(Itor=Historys.begin(); Itor!=Historys.end(); )
    {
        if(((History *)(*Itor))->HistoryID == HID)
            return &Itor;

        Itor++;
    }

    return NULL;
}

History* Book::GetHistoryByID(uint32_t HID)
{
    list<History*>::iterator* pItor = NULL;

    pItor = GetHistoryIteratorByID(HID);

    if(pItor == NULL)
        return NULL;
    else
        return (**pItor);
}

BPcode Book::DeleteHistoryByID(uint32_t HistoryID)
{
    list<History*>::iterator* pItor;

    pItor = GetHistoryIteratorByID(HistoryID);
    if(pItor == NULL)
    {
        bplog(LOG_LEVEL_WARN,"Book:This history don't existed in this book by id %d",HistoryID);
        return BP_DUPLICATE;
    }

    Historys.erase(*pItor);
    return BP_OK;
}

enum _BookStatus BookInfo::CharsToStatus(Bupchar status)
{
    if(!strcmp((const char *)status,"已读"))
        return BookRead;
    else if(!strcmp((const char *)status,"未读"))
        return BookUnread;
    else if(!strcmp((const char *)status,"感兴趣的"))
        return BookInterested;
    else if(!strcmp((const char *)status,"正在读"))
        return BookReading;
}

string BookInfo::StatusToString(enum _BookStatus status)
{
    string Status = "未读";

    if(status == BookRead)
        Status = "已读";
    else if(status == BookInterested)
        Status = "感兴趣的";
    else if(status == BookReading)
        Status = "正在读";
    else if(status == BookUnread)
        Status = "未读";

    return Status;
}

string BookInfo::BookFilePathToName(string path)
{
    string name;
    size_t pos = 0;

    pos = path.rfind('\/');

    return path.substr(pos+1);
}
